using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using System;
namespace WPFExcelReport
{
    public partial class VQPSTEP
    {
        public readonly static VQPSTEP Instance = new VQPSTEP();

        private string _QPLAN; public string QPLAN { get { return _QPLAN; } set { _QPLAN = value; OnPropertyChanged("QPLAN", value); } }
        private bool _FTAUTOQC;
        public bool FTAUTOQC
        {
            get { return _FTAUTOQC; }
            set { _FTAUTOQC = value; OnPropertyChanged("FTAUTOQC", value); }
        }
        public double LINE = 0;
        private void GetPOS()
        {
            var qData = from XD in TBMain.AsEnumerable()
                        group XD by new
                        {
                            KRS = XD["QPSPNRKRS"].ToString(),
                            LFD = XD["QPSPLFDNR"].ToString(),
                        }
                            into g
                            select new
                            {
                                KEY = g.Key,
                                LPOS = g.Max(XD => Convert.ToDouble(XD["QPSSTEP"].ToString())),
                            };
            foreach (var XD in qData)
            {
                LINE = XD.LPOS;
            }
        }
        public void NEWQPS()
        {
            if (LFD.Trim().Length == 0)
                return;
            TBMain.Clear();
            dwData = TBMain.NewRow();
            SetUpNewRow(ref dwData);
            dwData["QPSPNRKRS"] = KRS;
            dwData["QPSPLFDNR"] = LFD;
            GetPOS();
            LINE++;
            dwData["QPSSTEP"] = Helpers.Helper.Right("0000" + LINE, 4);
            Insert(dwData);
        }
        public void COPYQPS(DataRow _dwQP)
        {
            if (_dwQP == null)
                return;
            dwData = TBMain.NewRow();
            dwData.ItemArray = _dwQP.ItemArray;
            GetPOS();
            LINE++;
            dwData["QPSSTEP"] = Helpers.Helper.Right("0000" + LINE, 4);
            Insert(dwData);
        }
        public void Insert(DataRow _dwQP)
        {
            if (_dwQP == null)
                return;
            _dwQP["QPSNEUB"] = app.User;
            _dwQP["QPSNEUD"] = date;
            InsertQPSTEP(_dwQP, app.company);
            TBMain.Rows.InsertAt(_dwQP, 0);
        }
        public void SAVEQP(DataRow _dwQP)
        {
            if (_dwQP == null)
                return;
            _dwQP["QPPAENB"] = app.User;
            _dwQP["QPPAEND"] = date;
            UpdateQPSTEP(_dwQP, app.company, 0);
        }
    }
}
